package com.cc.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cc.blog.entity.SysUser;
import com.cc.blog.mapper.SysUserMapper;

import com.cc.blog.service.SysUserService;

import com.cc.blog.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public UserVo findUserVoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setNickname("爱学习的小蔡");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        return userVo;
    }

    @Override
    public SysUser findUserById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setNickname("爱学习的小蔡");
        }
        return sysUser;
    }

    @Override
    public SysUser finduser(String account, String password) {
        LambdaQueryWrapper<SysUser> lqb = new LambdaQueryWrapper<>();
        lqb.eq(SysUser::getAccount,account)
                .eq(SysUser::getPassword,password)
                .select(SysUser::getId,SysUser::getAccount,SysUser::getAvatar,SysUser::getNickname)
                .last("limit 1");
        return sysUserMapper.selectOne(lqb);
    }

    @Override
    public void saveUser(SysUser sysUser) {
        /**
         * 保存用户是id自增的，设计数据库时设定，用雪花算法
         */
         sysUserMapper.insert(sysUser);

    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getAccount,account).last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(lqw);
        return sysUser;
    }


}
